package com.huadao.pda.common.dialog;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;


import com.huadao.pda.R;
import com.huadao.pda.common.TimeFormat;
import com.huadao.pda.common.dialog.effects.RockButtonAttr;
import com.huadao.pda.data.bean.Dispensing;

import java.util.Calendar;

public class DialogFragmentHelper {

    private static final String DIALOG_POSITIVE = "确定";
    private static final String DIALOG_NEGATIVE = "取消";

    private static final String TAG_HEAD = DialogFragmentHelper.class.getSimpleName();

    /**
     * 加载中的弹出窗
     */
    private static final int PROGRESS_THEME = R.style.Base_AlertDialog;
    private static final String PROGRESS_TAG = TAG_HEAD + ":progress";

    public static CommonDialogFragment showProgress(FragmentManager fragmentManager, String message) {
        return showProgress(fragmentManager, message, true, null);
    }

    public static CommonDialogFragment showProgress(FragmentManager fragmentManager, String message, boolean cancelable) {
        return showProgress(fragmentManager, message, cancelable, null);
    }

    public static CommonDialogFragment showProgress(FragmentManager fragmentManager, final String message, boolean cancelable
            , CommonDialogFragment.OnDialogCancelListener cancelListener) {

        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                ProgressDialog progressDialog = new ProgressDialog(context, PROGRESS_THEME);
                progressDialog.setMessage(message);
                return progressDialog;
            }
        }, cancelable, cancelListener);
        dialogFragment.show(fragmentManager, PROGRESS_TAG);
        return dialogFragment;
    }

    /**
     * 简单提示弹出窗
     */
    private static final int TIPS_THEME = R.style.Base_AlertDialog;
    private static final String TIPS_TAG = TAG_HEAD + ":tips";

    public static void showTips(FragmentManager fragmentManager, String message) {
        showTips(fragmentManager, message, true, null);
    }

    public static void showTips(FragmentManager fragmentManager, String message, boolean cancelable) {
        showTips(fragmentManager, message, cancelable, null);
    }

    public static void showTips(FragmentManager fragmentManager, final String message, boolean cancelable
            , CommonDialogFragment.OnDialogCancelListener cancelListener) {

        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context, TIPS_THEME);
                builder.setMessage(message);
                return builder.create();
            }
        }, cancelable, cancelListener);
        dialogFragment.show(fragmentManager, TIPS_TAG);
    }

    /**
     * 选择日期
     */
    public static DialogFragment showDateDialog(FragmentManager fragmentManager, final String title, final Calendar calendar
            , final IDialogResultListener<Calendar> resultListener, final boolean cancelable) {
        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                final DatePickerDialog datePickerDialog = new DatePickerDialog(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        calendar.set(year, month, dayOfMonth);
                        resultListener.onDataResult(calendar);
                    }
                }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

                datePickerDialog.setTitle(title);
                datePickerDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        datePickerDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(DIALOG_POSITIVE);
                        datePickerDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setText(DIALOG_NEGATIVE);
                    }
                });
                return datePickerDialog;

            }
        }, cancelable, null);
        dialogFragment.show(fragmentManager, null);
        return null;
    }

    /**
     * 选择时间
     */
    public static void showTimeDialog(FragmentManager manager, final String title, final Calendar calendar, final IDialogResultListener<Calendar> resultListener, final boolean cancelable) {
        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @Override
            public Dialog getDialog(Context context) {
                final TimePickerDialog dateDialog = new TimePickerDialog(context, R.style.Base_AlertDialog, new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        if (resultListener != null) {
                            calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                            calendar.set(Calendar.MINUTE, minute);
                            resultListener.onDataResult(calendar);
                        }
                    }
                }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);

                dateDialog.setTitle(title);
                dateDialog.setOnShowListener(new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        dateDialog.getButton(DialogInterface.BUTTON_POSITIVE).setText(DIALOG_POSITIVE);
                        dateDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setText(DIALOG_NEGATIVE);
                    }
                });

                return dateDialog;
            }
        }, cancelable, null);
        dialogFragment.show(manager, null);
    }

    public static void showMessageDialog(Context context, String title, String message) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .addButton(context.getString(R.string.positive), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        rockDialog.dismiss();
                    }
                })
                .create().show();
    }

//    public static void showMaterialDialog(Context context, Dispensing material, boolean canReceive, final IDialogResultListener<Integer> listener) {
//        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_material, null);
//        TextView tv_materialNumber = customView.findViewById(R.id.tv_materialNumber);
//        tv_materialNumber.setText(String.valueOf(material.getBarCode()));
//        TextView tv_materialName = customView.findViewById(R.id.tv_materialName);
//        tv_materialName.setText(material.getName());
//        TextView tv_batchNumber = customView.findViewById(R.id.tv_batchNumber);
//        tv_batchNumber.setText(material.getBatchCode());
//        TextView tv_specifications = customView.findViewById(R.id.tv_specifications);
//        tv_specifications.setText(material.getSpecification());
//        TextView tv_expiryDate = customView.findViewById(R.id.tv_expiryDate);
//        if (material.getExpirationDate() != null) {
//            tv_expiryDate.setText(TimeFormat.date_sdf.format(material.getExpirationDate()));
//        }
//        TextView tv_note = customView.findViewById(R.id.tv_note);
//        tv_note.setText(material.getRemark());
//
//        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
//        dialogBuilder.withTitle("物料领取");
//        if (!material.isUse() && canReceive) {
//            dialogBuilder.addButton(context.getString(R.string.negative), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
//                @Override
//                public void onClick(RockDialog rockDialog, View view) {
//                    if (listener != null) {
//                        listener.onDataResult(new Integer(DialogInterface.BUTTON_NEGATIVE));
//                    }
//                    rockDialog.dismiss();
//                }
//            }).addButton(context.getString(R.string.receiptConfirm), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
//                @Override
//                public void onClick(RockDialog rockDialog, View view) {
//                    if (listener != null) {
//                        listener.onDataResult(new Integer(DialogInterface.BUTTON_POSITIVE));
//                    }
//                    rockDialog.dismiss();
//                }
//            });
//        } else {
//            dialogBuilder.addButton(context.getString(R.string.positive), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
//                @Override
//                public void onClick(RockDialog rockDialog, View view) {
//                    rockDialog.dismiss();
//                }
//            });
//        }
//        dialogBuilder.withCustomView(customView)
//                .create().show();
//    }

    public static void showConfirmDialog(Context context, String title, String message, IDialogResultListener<Integer> listener, boolean cancelable) {
        showTwoOperateDialog(context, title, message, R.string.positive, R.string.negative, listener, cancelable);
    }

    public static void showInsertDialog(FragmentManager manager, final String title, final int inputType, final IDialogResultListener<String> resultListener, final boolean cancelable) {
        CommonDialogFragment dialogFragment = CommonDialogFragment.newInstance(new CommonDialogFragment.OnCallDialog() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public Dialog getDialog(Context context) {
                final EditText editText = new EditText(context);
                editText.setBackground(null);
                editText.setInputType(inputType);
                editText.setPadding(60, 40, 0, 0);
                AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.Base_AlertDialog);
                builder.setTitle(title);
                builder.setView(editText);
                builder.setPositiveButton(DIALOG_POSITIVE, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (resultListener != null) {
                            resultListener.onDataResult(editText.getText().toString());
                        }
                    }
                });
                builder.setNegativeButton(DIALOG_NEGATIVE, null);
                return builder.create();

            }
        }, cancelable, null);
        dialogFragment.show(manager, null);

    }

    public static void showTwoOperateDialog(Context context, String title, String message, int posRes, int negRes, final IDialogResultListener<Integer> listener, boolean cancelable) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .addButton(context.getString(negRes), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_NEGATIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(posRes), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_POSITIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .isCancelableOnTouchOutside(cancelable)
                .create().show();
    }

    public static void showThreeOperateDialog(final Context context, String title, String message, int posRes, int negRes, int neuRes, final IDialogResultListener<Integer> listener) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .addButton(context.getString(posRes), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_POSITIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(negRes), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_NEGATIVE);
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(neuRes), new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(DialogInterface.BUTTON_NEUTRAL);
                        }
                        rockDialog.dismiss();
                    }
                }).create().show();
    }


    public static void showMultipleOperateDialog(Context context, String title, String message, final String[] operateList, final IDialogResultListener<Integer> listener, boolean isButtonList, boolean cancelable) {
        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        for (int i = 0; i < operateList.length; i++) {
            final int position = i;
            dialogBuilder.addButton(operateList[i], new RockCallBack() {
                @Override
                public void onClick(RockDialog rockDialog, View view) {
                    listener.onDataResult(position);
                    rockDialog.dismiss();
                }
            });
        }
        dialogBuilder.withTitle(title)
                .withMessage(message)
                .withButtonList(isButtonList)
                .isCancelableOnTouchOutside(cancelable).create().show();
    }

//    public static void doorOperateDialog(Context context,IDialogResultListener<Integer> listener){
//        String[] door_items = new String[]{context.getString(R.string.open_door), context.getString(R.string.close_door), context.getString(R.string.read), context.getString(R.string.stop)};
//        DialogFragmentHelper.showMultipleOperateDialog(context, context.getString(R.string.door), null, door_items, listener, true, true);
//    }

    public static void showNormalInputDialog(Context context, String title, String placeHolder, String defaultText, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_edittext, null);
        final EditText editText = customView.findViewById(R.id.editText);
        editText.setHint(placeHolder);
        if (defaultText != null) {
            editText.setText(defaultText);
            editText.setSelection(defaultText.length());
        }

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .addButton(context.getString(R.string.negative), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(R.string.positive), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Object[] res = new Object[2];
                            res[0] = DialogInterface.BUTTON_POSITIVE;
                            CharSequence text = editText.getText();
                            if (text != null && text.length() > 0) {
                                res[1] = text.toString();
                            } else {
                                res[1] = null;
                            }
                            listener.onDataResult(res);
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .create().show();
    }

    public static void showInputDialog(Context context, String title, String paramName, String unit, String placeHolder, int inputType, String defaultInput, int max, int posRes, int negRes, final IDialogResultListener<Object[]> listener) {
        View customView = LayoutInflater.from(context).inflate(R.layout.dialog_edittext, null);
        final EditText editText = customView.findViewById(R.id.editText);
        TextView textView = customView.findViewById(R.id.textView);
        TextView tv_unit = customView.findViewById(R.id.unit);
        textView.setText(paramName);
        tv_unit.setText(unit);
        if (defaultInput != null) {
            editText.setText(defaultInput);
            editText.setSelection(defaultInput.length());
        }
        editText.setInputType(inputType);
//        if (inputType == InputType.TYPE_CLASS_NUMBER) {
//            editText.addTextChangedListener(new MaxNumberTextWatch(max, editText, false, numberMaxAttention));
//        } else if (inputType == (InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER)) {
//            editText.addTextChangedListener(new MaxDecimalTextWatch(max, 2, editText, false, numberMaxAttention));
//        }
        editText.setHint(placeHolder);

        RockDialog.RockDialogBuilder dialogBuilder = new RockDialog.RockDialogBuilder(context);
        dialogBuilder.withTitle(title)
                .addButton(context.getString(negRes), RockButtonAttr.BUTTON_NEGATIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            listener.onDataResult(new Object[]{DialogInterface.BUTTON_NEGATIVE});
                        }
                        rockDialog.dismiss();
                    }
                })
                .addButton(context.getString(posRes), RockButtonAttr.BUTTON_POSITIVE, new RockCallBack() {
                    @Override
                    public void onClick(RockDialog rockDialog, View view) {
                        if (listener != null) {
                            Object[] res = new Object[2];
                            res[0] = DialogInterface.BUTTON_POSITIVE;
                            CharSequence text = editText.getText();
                            if (text != null && text.length() > 0) {
                                res[1] = text.toString();
                            } else {
                                res[1] = null;
                            }
                            listener.onDataResult(res);
                        }
                        rockDialog.dismiss();
                    }
                })
                .withCustomView(customView)
                .create().show();
    }

}


